#include "gamescene.h"
#include "gameconfig.h"
#include "gamecontroller.h"
#include "gamepushbutton.h"
#include "gamecoin.h"
#include "dataconfig.h"

#include <QIcon>
#include <QPixmap>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QPainter>
#include <QLabel>
#include <QFont>
#include <QPushButton>
#include <QDebug>
#include <QPropertyAnimation>


Gamescene::Gamescene(int level ,QWidget *parent)
    : QMainWindow(parent)
    ,m_level(level)
    ,m_lblwinGame(nullptr)
{
    this->setWindowTitle("金币游戏");
    this->setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    this->setWindowIcon(QIcon(IMG_GAME_ICON));


    //menu
    QMenuBar *lvMenuBar = this->menuBar();
    QMenu *menuFile = lvMenuBar->addMenu("文件");
    QAction *acBack = menuFile->addAction("返回");
    connect(acBack,&QAction::triggered,this,[=]()
    {
        GameController::getInstance()->deletGameScene();
        GameController::getInstance()->showLevelScene();
    });

    QAction *acQuit = menuFile->addAction("退出");
    connect(acQuit,&QAction::triggered,this,[=]()
    {
        exit(0);
    });

    //游戏胜利标志
    m_lblwinGame = new QLabel(this);
    QPixmap winPix(IMG_LEVEL_COMPLETE);
    m_lblwinGame->setPixmap(winPix);
    m_lblwinGame->setGeometry(0,GAME_HEIGHT +100,winPix.width(),winPix.height());
    m_lblwinGame->move((GAME_WIDTH -m_lblwinGame->width())/2,-m_lblwinGame->height());

    //游戏区背
    dataConfig gameDataInfo;
    for(int i = 0;i<4;i++)
    {
        for(int j = 0 ; j<4;j++)
        {
            //
            m_gameInfoArray[i][j] = gameDataInfo.mData[m_level][i][j];
            //背景
            QLabel *lbBgCoin = new QLabel(this);
            QPixmap bgCoinPix(IMG_BOOER_NODE);
            lbBgCoin->setPixmap(bgCoinPix);
            lbBgCoin->setGeometry(0,0,bgCoinPix.width(),bgCoinPix.height());
            lbBgCoin->move(95 + i*50 , 200 + j*50);
            //背景.金币
            QString strCoin = "";
            if(m_gameInfoArray[i][j] == 1)
            {
                strCoin = ":/res/Coin0001.png";
            }
            else
            {
                strCoin = ":/res/Coin0008.png" ;
            }
            m_btnCoin[i][j] = new GameCoin(strCoin,lbBgCoin);
            m_btnCoin[i][j]->move(2,4);
            m_btnCoin[i][j]->setY(j);
            m_btnCoin[i][j]->setX(i);
            m_btnCoin[i][j]->setFlag(m_gameInfoArray[i][j]);

            connect(m_btnCoin[i][j],&QPushButton::clicked,this,[=]()
            {
                //改变点击的金币状态
                GameController::getInstance()->playCoinSound();
                m_gameInfoArray[i][j]  =   m_gameInfoArray[i][j] ==1 ? 0 : 1;
                m_btnCoin[i][j]->changeFlag();
                QTimer::singleShot(150,this,[=]()
                {
                    //改变点击金币周围金币的状态
                    int xIndex = m_btnCoin[i][j]->posX();
                    int yIndex = m_btnCoin[i][j]->posY();
                    if(xIndex>0)
                    {
                        m_gameInfoArray[xIndex -1][yIndex] = m_gameInfoArray[xIndex -1][yIndex] ==1 ? 0 : 1;
                        m_btnCoin[xIndex -1][yIndex]->changeFlag();
                    }
                    if(xIndex<3)
                    {
                        m_gameInfoArray[xIndex +1][yIndex] = m_gameInfoArray[xIndex +1][yIndex] ==1 ? 0 : 1;
                        m_btnCoin[xIndex +1][yIndex]->changeFlag();
                    }

                    if(yIndex>0)
                    {
                        m_gameInfoArray[xIndex][yIndex -1] = m_gameInfoArray[xIndex][yIndex -1] ==1 ? 0 : 1;
                        m_btnCoin[xIndex][yIndex -1]->changeFlag();
                    }
                    if(yIndex<3)
                    {
                        m_gameInfoArray[xIndex][yIndex +1] = m_gameInfoArray[xIndex][yIndex +1] ==1 ? 0 : 1;
                        m_btnCoin[xIndex][yIndex +1]->changeFlag();
                    }

                    //判断胜负
                    bool isWin = true;
                    for(int m = 0;m<4;m++)
                    {
                        for(int n = 0;n<4;n++)
                        {
                            if(m_gameInfoArray[m][n] == 0)
                            {
                                isWin = false;
                                break;
                            }

                        }
                    }
                    if(isWin ==true)
                    {
                        GameController::getInstance()->playWinSound();
                        qDebug()<<"恭喜你，游戏胜利";
                        //胜利动画
                        QPropertyAnimation *winAnimation = new QPropertyAnimation(m_lblwinGame,"geometry");
                        winAnimation->setDuration(1000);
                        winAnimation->setStartValue(QRect(m_lblwinGame->x(),m_lblwinGame->y(),
                                                    m_lblwinGame->width(),m_lblwinGame->height()));
                        winAnimation->setEndValue(QRect(m_lblwinGame->x(),40,
                                                    m_lblwinGame->width(),m_lblwinGame->height()));
                        winAnimation->setEasingCurve(QEasingCurve::OutBounce);
                        winAnimation->start();
                        //设置金币不可点击
                        for (int m = 0;m<4;m++)
                        {
                            for(int n =0 ;n<4;n++)
                            {
                                m_btnCoin[m][n]->setwin(true);
                            }
                        }
                    }


                });

            });

        }
    }



    //等级信息
    QLabel *lblevel = new QLabel(this);
    lblevel->setText(QString("LEVE_%1").arg(m_level));//根据传入的数据进行显示
    lblevel->setGeometry(10,GAME_HEIGHT-80,180,50);
    QFont levefont;
    levefont.setFamily("中华新魏");
    levefont.setBold(true);
    levefont.setPointSize(20);
    lblevel->setFont(levefont);


    //返回按钮
    GamePushButton *btnBak = new GamePushButton(BMG_BACK_NORMAL,BMG_BACK_PRESSED);
    btnBak->setParent(this);
    btnBak->move(GAME_WIDTH-btnBak->width(),GAME_HEIGHT-btnBak->height());
    connect(btnBak,&QPushButton::clicked,this,[=]()
    {
        GameController::getInstance()->playBackSound();
        GameController::getInstance()->deletGameScene();
        GameController::getInstance()->showLevelScene();
    });


}





void Gamescene::paintEvent(QPaintEvent *event)
{
    QPainter gamePainter(this);
    QPixmap gamePix;
    bool retBg = gamePix.load(IMG_BG_GAMESCENE);
    if(retBg)
    {
        gamePainter.drawPixmap(0,0,gamePix);
    }
    //
    bool retTitle = gamePix.load(IMG_GAME_TITLE);
    if(retTitle)
    {
        gamePix = gamePix.scaled(gamePix.width()*0.7,gamePix.height()*0.7);//缩放图片
        gamePainter.drawPixmap(10,30,gamePix);
    }
}
